package com.weblog.user.dao;

import com.weblog.model.user.Admin;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface AdminDao extends JpaRepository<Admin,String>, JpaSpecificationExecutor<Admin> {

    /**
     * 登录，登录用户名查询对象
     * @param username
     * @return
     */
    Admin findByUsername(String username);

    /**
     * 修改管理员账号
     * @param loginname
     * @param id
     */
    @Modifying
    @Query(value = "update tb_admin set username=?1 where id =?2",nativeQuery = true)
    void updateUsername(String loginname, String id);

    /**
     * 拉黑
     * @param id
     * @return
     */
    @Modifying
    @Query(value = "UPDATE tb_admin SET state = 0 WHERE id=?1",nativeQuery = true)
    int blackAdminById(String id);

    /**
     * 根据登录名查询管理员且id不等于参数中的id
     * @param username
     * @param id
     * @return
     */
    Admin findAllByUsernameAndIdNot(String username, String id);

    @Query(value = "select state from tb_admin where id=?1",nativeQuery = true)
    String findStateById(String id);
}
